package com.neu.admin;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.neu.common.RequiresPermissionsDesc;
import com.neu.common.validator.Order;
import com.neu.common.validator.Sort;
import com.neu.doc.AdminAdController_ResOK;
import com.neu.doc.AdminIssueController_ResOk;
import com.neu.doc.AdminIssueController_list;
import com.neu.doc.ResponseOK;
import com.neu.domain.ShoppingAd;
import com.neu.domain.ShoppingIssue;
import com.neu.domain.ShoppingSearchHistory;
import com.neu.service.ShoppingIssueService;
import com.neu.util.ResponseUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import javax.validation.constraints.NotNull;
import java.util.List;
@RestController
@RequestMapping("/admin/issue")
@Validated
public class AdminIssueController {
    private final Log logger = LogFactory.getLog(AdminIssueController.class);

    @Autowired
    private ShoppingIssueService shoppingIssueService;
    @RequiresPermissions("admin:issue:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "通用问题"}, button = "查询")
    @GetMapping("/list")
    public Object list(String question, String answer,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @Sort @RequestParam(defaultValue = "add_time") String sort,
            @Order @RequestParam(defaultValue = "desc") String order) {
    		List<ShoppingIssue> BrandList = shoppingIssueService.querySelective(question,answer, page, limit, sort, order);
    		return ResponseUtil.okList(BrandList);
    }
    private Object validate(ShoppingIssue issue) {
        String question = issue.getQuestion();
        if (StringUtils.isEmpty(question)) {
            return ResponseUtil.badArgument();
        }
        String answer = issue.getAnswer();
        if (StringUtils.isEmpty(answer)) {
            return ResponseUtil.badArgument();
        }
        return null;
    }
    @RequiresPermissions("admin:issue:create")
    @RequiresPermissionsDesc(menu = {"商场管理", "通用问题"}, button = "添加")
    @PostMapping("/create")
    public Object create(@ApiParam(name="ShoppingAd對象",value="传入json格式",required=true)
	@RequestBody ShoppingIssue record) {
        Object error = validate(record);
        if (error != null) {
            return error;
        }
        shoppingIssueService.add(record);
        return ResponseUtil.ok(record);
    }
    @ApiOperation(value="修改", notes="修改通用问题管理", response = AdminIssueController_ResOk.class)
    @RequiresPermissions("admin:issue:update")
    @RequiresPermissionsDesc(menu = {"商场管理", "通用问题"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@ApiParam(name="ShoppingAd對象",value="传入json格式",required=true)
	@RequestBody ShoppingIssue record) {
    	  Object error = validate(record);
          if (error != null) {
              return error;
          }
          if (shoppingIssueService.updateById(record) == 0) {
              return ResponseUtil.updatedDataFailed();
          }
          return ResponseUtil.ok(record);
    	
    }


    @ApiOperation(value="删除", notes="删除通用问题管理", response = AdminIssueController_ResOk.class)
    @RequiresPermissions("admin:issue:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "通用问题"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@ApiParam(name="ShoppingAd對象",value="传入json格式",required=true)
	@RequestBody ShoppingIssue record) {
    	 Integer id = record.getId();
         if (id == null) {
             return ResponseUtil.badArgument();
         }
         shoppingIssueService.deleteById(id);
         return ResponseUtil.ok();
    	
    }

}
